﻿using System;
using System.Web.Mvc;

using Whoever.Framework.Utils;
using Whoever.Framework.CustomException;
using Whoever.Patterns.MVC.Filters;
using Whoever.Patterns.MVC.Models;

using EditorialProyectos.Website.Models;
using EditorialProyectos.Business;
using EditorialProyectos.Business.Entities;

using Whoever.Framework.Enum;

namespace EditorialProyectos.Website.Controllers
{
    public class CategoryController : Controller
    {
        public ActionResult Main()
        {
            var model = new CategoryViewModel();

            model.List = ManagerFactory.CategoryManager.GetAll();

            return View(model);
        }

        [AJAXExceptionHandler]
        public ActionResult RebindList()
        {
            return Json(ManagerFactory.CategoryManager.GetAll());
        }

        [AJAXExceptionHandler]
        public ActionResult Create(Category entity)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ManagerFactory.CategoryManager.Insert(entity);

                    return Json(new AJAXResponse { Message = Localized.Message("Create", MessageType.Success), MessageType = MessageType.Success, Success = true, MustRedirect = false });
                }
                throw new BusinessException(Localized.Message("Model", MessageType.Error));
            }
            catch (BusinessException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }

        [AJAXExceptionHandler]
        public ActionResult Update(Category entity)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var item = ManagerFactory.CategoryManager.GetById(entity);

                    if (TryUpdateModel(item))
                        ManagerFactory.CategoryManager.Update(item);

                    return Json(new AJAXResponse { Message = Localized.Message("Update", MessageType.Success), MessageType = MessageType.Success, Success = true, MustRedirect = false });
                }
                throw new BusinessException(Localized.Message("Model", MessageType.Error));
            }
            catch (BusinessException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }

        [AJAXExceptionHandler]
        public ActionResult Delete(Category entity)
        {
            try
            {
                ManagerFactory.CategoryManager.Delete(entity);

                return Json(new AJAXResponse { Message = Localized.Message("Delete", MessageType.Success), MessageType = MessageType.Success, Success = true, MustRedirect = false });
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }
    }
}
